package com.xgzit.xgzmall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xgzit.xgzmall.coupon.entity.SeckillSkuRelationEntity;
import com.xgzit.xgzmall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgzit.common.utils.PageUtils;
import com.xgzit.common.utils.Query;

import com.xgzit.xgzmall.coupon.dao.SeckillSessionDao;
import com.xgzit.xgzmall.coupon.entity.SeckillSessionEntity;
import com.xgzit.xgzmall.coupon.service.SeckillSessionService;
import org.springframework.util.CollectionUtils;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    SeckillSkuRelationService skuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLatestThreeDaySession() {
        // 计算最近3天  当天 00:00:00  到 第三天 23:59:59
        String startTime = startTime();
        String endTime = endTime();
        // 查询活动
        // SELECT * from sms_seckill_session WHERE start_time BETWEEN '' AND ''
        List<SeckillSessionEntity> list = this.list(new LambdaQueryWrapper<SeckillSessionEntity>()
                .between(SeckillSessionEntity::getStartTime, startTime, endTime));
        // 设置商品
        if (Objects.nonNull(list) && !CollectionUtils.isEmpty(list)){
            return list.stream().peek(session -> {
                Long id = session.getId();
                List<SeckillSkuRelationEntity> relationEntities = skuRelationService.list(new LambdaQueryWrapper<SeckillSkuRelationEntity>()
                        .eq(SeckillSkuRelationEntity::getId, id));
                session.setRelationSkus(relationEntities);
            }).collect(Collectors.toList());
        }
        return null;
    }

    /**
     * 最近三天的起始时间
     **/
    private String startTime() {
        return LocalDateTime.of(LocalDate.now(), LocalTime.MIN)
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 最近三天的结束时间
     **/
    private String endTime() {
        return LocalDateTime.of(LocalDate.now().plusDays(2), LocalTime.MAX)
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
}
